'''###########################################################################################################################
### File: level.py
### Name: Patrick Delaney, Andrew Oberlin
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Final Project
### Description: Class for saving and loading the levels
################
'''
import cPickle as serialize
from visual_object_3d import VisualObjectSave

class Level:
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Initializes the level object
	'''
	def __init__(self):
		self.name = ""
		self.objects = {}
		self.mainCharacter = None
		
	'''
	### Name: setName
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Sets the name for this level
	'''
	def setName(self, name):
		self.name = name.capitalize()
		
	'''
	### Name: getName
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Gets the name for this level
	'''
	def getName(self):
		return self.name
	
		
	
	'''
	### Name: setObjects
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Sets the object array for this level
	'''
	def setObjects(self, objects):
		self.objects = {}
			
		for key in objects.keys():
			self.objects[key] = VisualObjectSave(objects[key])
		
	'''
	### Name: getObjects
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Returns the object array for this level
	'''
	def getObjects(self):
		return self.objects
		
	'''
	### Name: setMainCharacter
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Sets the main character for this level
	'''
	def setMainCharacter(self, newMainChar):
		self.mainCharacter = VisualObjectSave(newMainChar)
		
	'''
	### Name: getMainCharacter
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Returns the omain character for this level
	'''
	def getMainCharacter(self):
		return self.mainCharacter
	
	'''
	### Name: save
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Saves this character as a serialized file
	###				 with name 'projects/<projectName>/characters/main/<name>.char' 
	###				 **name attribute required**
	'''
	def save(self, projectName):
		if (self.name):
			file = open( "projects/" + projectName + "/levels/" + self.name +  ".lvl", "wb" )
			serialize.dump(self, file, 1)
			
	'''
	### Name: load
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Loads the character from a serialized file
	###				 with name 'projects/<projectName>/characters/main/<name>.char'
	'''
	def load(self, path):
		try:
			file = open( path, "rb" )
			newSelf = serialize.load(file)
			self.copy(newSelf)
		except IOError, e:
			print "Problem loading the level"
			print e
			
	'''
	### Name: copy
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Copies the newSelf's attributes to self
	'''
	def copy(self, newSelf):
		self.name = newSelf.name
		self.objects = newSelf.objects
		self.mainCharacter = newSelf.mainCharacter
		self.mainCharacter = newSelf.mainCharacter
		